package demo1;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public boolean chkPalindrome(ListNode A) {
        ListNode head = A;
        ListNode curN = A;
        while(curN != null && curN.next != null){
            head = head.next;
            curN = curN.next.next;
        }
          curN = head;
        curN = swap(curN);
        while(A != head){
            if(A.val != curN.val){
                return false;
            }
            A = A.next;
            curN = curN.next;
        }
        return true;
    }
    public ListNode swap(ListNode head){
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode prev = cur.next;
            cur.next = head;
            head = cur;
            cur = prev;
        }
        return head;

    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int A = size(headA);
        int B = size(headB);
        int C = Math.abs(A-B);
        if(A > B){
            for(int i = 0 ; i < C;i++){
                headA = headA.next;
            }
        }else {
            for(int i = 0 ; i < C;i++){
                headB = headB.next;
            }
        }
        while(headA != null){
            if(headA == headB){
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
    public int size(LisNode head){
        int count = 0;
        while(head != 0){
            head = head.next;
            count++;
        }
        return count;
    }
    public boolean hasCycle(ListNode head) {
      ListNode cur = head;
      while(cur != null && cur.next != null){
          cur = cur.next.next;
          head = head.next;
          if(cur == head){
              return true;
          }
      }
      return false;
    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode node = new ListNode(1);
        ListNode head = node;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                head.next = list1;
                list1 = list1.next;
            }else {
                head.next = list2;
                list2 = list2.next;

            }
            head = head.next;
        }
        if(list1 == null){
            head.next = list2;
        }
        if(list2 == null){
            head.next =list1;
        }
        return node.next;
    }
}